
var UpdateController = function($scope, $http, $modal, $upload){
	
	$http.defaults.headers.post["Content-Type"] = "application/json";
	var urlBase="http://localhost:8080/finalproject";
	
	$scope.getListUser = function(){
		$scope.resetError();
		$http.get(urlBase + '/updateinfo/getlist').success(function(respone){
			$scope.listUsers = respone;
		}).error(function(){
			$scope.setError("Can't get List User");
		});
	}
	
	$scope.openInfo = function(id){
		$scope.resetError();
		$http.post(urlBase + '/updateinfo/get/' + id).success(function(respone){
			var modalInstance = $modal.open({
                templateUrl: 'update.html',
                controller: $scope.ModalInstanceCtrl,
                resolve: {
                  user: function () {
                    return respone;
                  },
                  $rootScope: function(){
                      return $scope;
                  },
                  $upload: function(){
                	  return $upload;
                  }
                }
           });
		}).error(function(){
			$scope.setError("Can't view user");
		});
	}
	
	$scope.ModalInstanceCtrl = function ($scope, $http, $modalInstance, user, $rootScope, $upload) {
		
		  $scope.dataUrls = [];
		
          $scope.user = user;

          $scope.updateUser = function (user) {
        	  if(typeof $scope.password == 'undefined'){
         		 $http.post(urlBase + '/updateinfo/passwordold/updateuser', user).success(function(respone){
         			 $rootScope.getListUser();
         			 $rootScope.setError("Update user successfully");
         		 }).error(function(respone){
         			 $rootScope.setError("Can't update user");
         		 });
         	 } else {
         		 am.password = $scope.password;
         		 $http.post(urlBase + '/updateinfo/passwordnew/updateuser', user).success(function(respone){
         			 $rootScope.getListUser();
         			 $rootScope.setError("Update user successfully");
         		 }).error(function(respone){
         			 $rootScope.setError("Can't update user");
         		 });
         	 }
              
              $modalInstance.close();
          };
          
          $scope.cancel = function () {
              $modalInstance.dismiss('cancel');
          };
          
          $scope.onFileSelect = function($files){
         	 var $file = $files[0];
              if(window.FileReader && $file.type.indexOf('image') > -1){
                      var fileReader = new FileReader();
                      fileReader.readAsDataURL($file);
                      
                      fileReader.onload = function(e){
                              $scope.dataUrls['imagenew'] = e.target.result;
                      }
              }
              
              $scope.upload = $upload.upload({
                      url: urlBase + '/updateinfo/file/upload',
              }).progress(function(evt){
                      $scope.percent = parseInt(100.0 * evt.loaded / evt.total);
                       console.log('percent: ' +  $scope.percent);
              }).success(function(respone){
             	 console.log($scope.dataUrls['imagenew']);
             	 	$scope.sendToDatabase($scope.dataUrls['imagenew']);
                      console.log("upload finished");
              }).error(function(){
                      console.log("upload failed");
              });
              
              
          };
          
          $scope.sendToDatabase = function(dataUrlsN){
              $http.post(urlBase + '/updateinfo/file/image', dataUrlsN).success(function(respone){
                      $scope.dataUrls['image'] = respone;
              }).error(function(){
                      alert("failed");
              })
          }
	}
	
	 $scope.setError = function(message){
         $scope.error = true;
         $scope.errorMessage = message;
	 }
	 
	 $scope.resetError = function(){
	         $scope.error = false;
	         $scope.errorMessage = '';
	 }
	 
	 $scope.getListUser();
}